﻿#include <qt_windows.h>
#include "qtservice.h"

/*!
    1. How to use
    QtService is a convenience class to use QtServiceBase class to
    develop service programs. Its basic usage is as follows:

    \list
        \o  Inherit from the QtService class and override its methods start(),
            stop(), resume(), pause(), processCommand().
        \o  Create an object of subclass of QtService in main(), and call the
            QtServiceBase::exec(). such as:
            \code
                int main(int argc, char *argv[]){
                    MyService myService(argc, argv,"MyService","Service Demo");
                    return myService.exec();
                }
            \endcode
    \endlist


    2. Control service lifecycle
    You can control the lifecycle of the service from the command line.
    When the user runs the .exe with arguments from the command line, the
    QtServiceBase::exec() willl be triggered.

    \list
        \o  if run with no argument or invalid argument, the service will be
            started, and the QtServiceBase::start() will be triggered. Returns
            exitCode 0 if successfully, otherwise returns -1.
        \o  if run with -t, the service will be terminated, and the
            QtServiceBase::stop() will be triggered. Returns exitCode 0
            if successfully, otherwise returns -1.
        \o  if run with -p, the service will be paused, and the
            QtServiceBase::pause() will be triggered. Returns exitCode 0
            if successfully, otherwise returns -1.
        \o  if run with -r, the service will be resumed, and the
            QtServiceBase::resume() will be triggered. Returns exitCode 0
            if successfully, otherwise returns -1.
        \o  if run with -i, the service will register itself in the system.
            Returns exitCode 0 if successfully, otherwise returns -1.
        \o  if run with -u, the service will unregister itself in the system.
            Returns exitCode 0 if successfully, otherwise returns -1.
        \o  if run with -v, the service will print itself state on the console.
            Returns exitCode 0.
        \o  if run with -c code, the service will receive the command code, and
            the QtServiceBase::processCommand() will be triggered.
        \o  if run with -e, the service wille be started as a standalone
            application (useful for debug purposes), and blocking.
    \endlist


    3. Service config file
    Due to operating system security restrictions, the server is restricted
    from working in the C:\Windows\System32 directory. So you can't use the
    normal API(such as QStandardPaths) to access the system directory. There
    are several ways to access other directories on the system:

    \list
        \o  Write the path you need to access to the system environment variable
            when installing program. And access the desired path by reading
            the environment variable.
        \o  If you want to store the config file in the exe directory, you can
            get the path by modulePath().
    \endlist


    4. Communicate with the service
    The service is taken over by the Service Control Manager (SCM) after started,
    which can no longer access standard output, error, or input. Therefore, if
    service wants to report status to the outside, it can write the data to a log
    file.
*/

QtService::QtService(int &argc, char **argv, QString serviceName, QString serviceDescription, QObject *parent):
    QObject(parent),
    QtServiceBase(argc,argv,serviceName)
{
    //set service description
    setServiceDescription(serviceDescription);

    //set service autostart
    setStartupType(QtServiceController::AutoStartup);
}

QString QtService::modulePath()
{
    wchar_t path[_MAX_PATH];
    ::GetModuleFileNameW( 0, path, sizeof(path) );
    return QString::fromUtf16((unsigned short*)path);
}

void QtService::createApplication(int &argc, char **argv)
{
    app = new QCoreApplication(argc, argv);
}

int QtService::executeApplication()
{
    return QCoreApplication::exec();
}

